﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Raise3000.Code.Attribute_Calculator;
using Raise3000.Code.Foundation;
using Raise3000.Code.Helpers;

namespace Raise3000.Code
{
    abstract public class IHandHistoryLoader
    {
        List<string> handHistoriesFiles_ = null;

        // #TODO: this temporaly hard-coded
        string playerTemplateNamePostfix_ = "_4seat_0.05/0.10";

        private int successfullyReadedHand_ = 0;

        protected CalculatorManager calculatorManager_ = null;

        public IHandHistoryLoader(List<string> handHistories, CalculatorManager calculatorManager)
        {
            handHistoriesFiles_ = new List<string>(handHistories);
            calculatorManager_ = calculatorManager;
        }

        public int GetSuccessfullyReadedHand()
        {
            return successfullyReadedHand_;
        }

        public void LoadHandHistories()
        {
            foreach (string handHistoryFileName in handHistoriesFiles_)
            {
                LoadHandHistory(handHistoryFileName);
            }
        }

        protected string GetPlayerTemplateName(string playerName)
        {
            return playerName + playerTemplateNamePostfix_;
        }

        protected void LoadHandHistory(string filename)
        {
            LoggerHelper loggerHelper = LoggerHelper.GetLoggerHelper();
            loggerHelper.LogMessage("Loading hand history from handhistory file: " + filename);

            StreamReader streamReader = new StreamReader(filename);

            successfullyReadedHand_ = 0;

            while (ReadNextHandFromStream(streamReader))
            {
                ++successfullyReadedHand_;
            }

            loggerHelper.LogMessage("Hands processed : " + successfullyReadedHand_.ToString());
            streamReader.Close();
        }

        abstract protected bool ReadNextHandFromStream(StreamReader sr);
    }
}
